package com.example.administrator.qimo3no74.category;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.qimo3no74.R;

import java.util.List;

public class CategoryItemAdapter extends RecyclerView.Adapter<CategoryItemAdapter.ViewHolder> {


    private List<CategoryItem> categoryItems;

    static  class ViewHolder extends RecyclerView.ViewHolder{
        TextView itemName;

        public ViewHolder(View itemView, final Context context){
            super(itemView);
            this.itemName = itemView.findViewById(R.id.categoryTextViewName);
        }
    }

    public CategoryItemAdapter(List<CategoryItem> categoryItems){
        this.categoryItems = categoryItems;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.category_item,viewGroup,false);
        ViewHolder holder = new ViewHolder(view,viewGroup.getContext());
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder,final int i) {
        final CategoryItem categoryItem = categoryItems.get(i);
        viewHolder.itemName.setText(categoryItem.getItemName());
        viewHolder.itemName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(view.getContext(),""+categoryItem.getItemName() + (i+1),Toast.LENGTH_SHORT).show();
                //myListener.massage(i,categoryItem.getItemName());
                if(onItemClickListener!=null){
                    onItemClickListener.OnItemClick(view,categoryItems.get(i),i);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return categoryItems.size();
    }

    public interface OnItemClickListener{
        public void OnItemClick(View view,CategoryItem data,int position);
    }

    public OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }

}
